package etp.client.gui;

import java.util.ArrayList;
import java.util.List;

import org.gwtwidgets.client.util.SimpleDateFormat;

import com.allen_sauer.gwt.dnd.client.drop.DropController;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.HasAllMouseHandlers;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import etp.client.Tarefa;

public class TaskBoxWidget extends Composite implements HasAllMouseHandlers {

	private Tarefa model;
	
	private List<TaskBoxWidget> subTarefas = new ArrayList<TaskBoxWidget>();
	
	private FlexTable pnlMain = new FlexTable();
	
	private DraggableHeader dndHeader = new DraggableHeader();
	
	private CheckBox chkSelect = new CheckBox();

	private Label lblCodigo = new Label("1");
	
	private Label lblInicio = new Label();
	
	private Label lblFim = new Label();
	
	private Label lblTempo = new Label();
	
	private Label lblNome = new Label();
	
	private ProgressBar pbrProgress = new ProgressBar();
	
	private SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
	
	private DropController dropController;
	
	private List<TaskBoxWidgetButtonsHandler> buttonHandlers = 
		new ArrayList<TaskBoxWidgetButtonsHandler>();
	
	public TaskBoxWidget(){
	
		this.initWidget(this.pnlMain);
		
		this.pnlMain.setWidget(0, 0, this.dndHeader);
		this.pnlMain.setWidget(0, 1, this.chkSelect);
		this.pnlMain.setWidget(0, 2, this.lblCodigo);
		this.pnlMain.setWidget(0, 3, this.lblNome);
		this.pnlMain.setWidget(0, 4, this.pbrProgress);
		this.pnlMain.setWidget(0, 5, this.lblInicio);
		this.pnlMain.setWidget(0, 6, this.lblFim);
		
		/*
		 * Tamanho
		 */
		this.pnlMain.getFlexCellFormatter().setWidth(0, 0, "10px");
		this.pnlMain.getFlexCellFormatter().setWidth(0, 1, "10px");
		this.pnlMain.getFlexCellFormatter().setWidth(0, 2, "30px");
		this.pnlMain.getFlexCellFormatter().setWidth(0, 3, "80%");
		this.pnlMain.getFlexCellFormatter().setWidth(0, 4, "100px");
		
		this.pnlMain.getFlexCellFormatter().setAlignment(0, 2, 	
				HasHorizontalAlignment.ALIGN_LEFT,
				HasVerticalAlignment.ALIGN_MIDDLE);
		
		this.pbrProgress.setWidth("100px");
		this.pbrProgress.setHeight("10px");
		/*
		 * Eventos de double click
		 */
		this.addDomHandler(new ActDoubleClick(), DoubleClickEvent.getType());
		
		/*
		 * Eventos de mouse
		 */
		this.addDomHandler(new ActMouseOver(), MouseOverEvent.getType());
		this.addDomHandler(new ActMouseOut(), MouseOutEvent.getType());
		
		/*
		 * Estilos
		 */
		this.setStyleName("etp-TaskBoxWidget");
		this.dndHeader.setStyleName("etp-DndHandler");
	}
	
	public void addButtonsHandler(TaskBoxWidgetButtonsHandler handler){
		this.buttonHandlers.add(handler);
	}
	
	public boolean removeButtonsHandler(TaskBoxWidgetButtonsHandler handler){
		return this.buttonHandlers.remove(handler);
	}
	
	public Tarefa getModel() {
		return model;
	}

	public void setModel(Tarefa model) {
		this.model = model;
		
		this.lblCodigo.setText(model.getOrdem() + "");
		
		this.lblNome.setText(model.getNome());
		
		this.lblTempo.setText(model.getTempoH()+"h");
		
		this.lblFim.setText(dateFormatter.format(model.getDataFim()));
		
		this.lblInicio.setText(dateFormatter.format(model.getDataInicio()));
		
		this.pbrProgress.setProgress((int)this.model.getPorcentagem());
		
	}

	public DropController getDropController() {
		return dropController;
	}

	public void setDropController(DropController dropController) {
		this.dropController = dropController;
	}

	public int getCodigo() {
		return Integer.parseInt(this.lblCodigo.getText());
	}

	public void setCodigo(int codigo) {
		this.lblCodigo.setText(codigo+"");
	}

	public Widget getDNDHeader(){
		return this.dndHeader;
	}
	
	protected void fireEditClicked(){
		for(TaskBoxWidgetButtonsHandler h : this.buttonHandlers){
			h.editClicked(this);
		}
	}
	
	protected void fireRemoveClicked(){
		for(TaskBoxWidgetButtonsHandler h : this.buttonHandlers){
			h.removeClicked(this);
		}
	}
	
	protected class ActMouseOver implements MouseOverHandler{

		public void onMouseOver(MouseOverEvent event) {
//			TarefaWidget.this.addStyleName("TarefaWidget_over");
//			TarefaWidget.this.pnlButtons.setVisible(true);
		}
		
	}
	
	protected class ActMouseOut implements MouseOutHandler{

		public void onMouseOut(MouseOutEvent event) {
//			TarefaWidget.this.pnlButtons.setVisible(false);
//			TarefaWidget.this.removeStyleName("TarefaWidget_over");
		}
		
	}
	
	protected class ActDoubleClick implements DoubleClickHandler{

		@Override
		public void onDoubleClick(DoubleClickEvent event) {
			TaskBoxWidget.this.fireEditClicked();
		}
		
	}
	
	public interface TaskBoxWidgetButtonsHandler{

		public void editClicked(TaskBoxWidget w);
		
		public void removeClicked(TaskBoxWidget w);
	}

	@Override
	public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
		return this.addDomHandler(handler, MouseDownEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
		return this.addDomHandler(handler, MouseUpEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return this.addDomHandler(handler, MouseOutEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return this.addDomHandler(handler, MouseOverEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
		return this.addDomHandler(handler, MouseMoveEvent.getType());
	}

	@Override
	public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
		return this.addDomHandler(handler, MouseWheelEvent.getType());
	}
	
}
